package com.pdg.mcplugin.spawnsurance.commandprocessors;

import org.bukkit.Material;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;

import com.pdg.mcplugin.common.ArgumentList;
import com.pdg.mcplugin.common.baseclasses.CommandProcessorBase;
import com.pdg.mcplugin.spawnsurance.PaySource;
import com.pdg.mcplugin.spawnsurance.FilterState;
import com.pdg.mcplugin.spawnsurance.SpawnSurance;

public class SetCommandProcessor extends CommandProcessorBase<SpawnSurance> {
	private static final String messageSuccess = "Set '%s' to '%s'.";

	public SetCommandProcessor(SpawnSurance thePlugin) {
		super(thePlugin, new String[]{
				"/spawnsurance set (property) (parameters)",
				"Available \"set\" Properties:",
				"  claims (amount) - current player's number of claims",
				"  claims-for (player name) (amount) - number of claims for player",
				"  material (item name) - material for spawnsurance cost",
				"  minimum (amount) - minimum cost for spawnsurance",
				"  maximum (amount) - maximum cost for spawnsurance",
				"  acceleration (amount) - acceleration for spawnsurance cost",
				"  message-enabled (flag) - enabled state of spawnsurance message (true|false)",
				"  message-text (text) - custom text of spawnsurance message",
				"  pay-source (pay source) - pay source of spawnsurance",
				"  filter-state (filter option) - set filter option (OFF|INCLUDE|EXCLUDE)"
		});
		setSubProcessor(Property.CLAIMS_FOR.getText(),new SetClaimsForCommandProcessor(thePlugin));
	}

	@Override
	protected boolean specialProcessCommand(CommandSender theCommandSender,
			String theSubCommand, ArgumentList theArgumentList) {
		
		Property property = Property.parse(theSubCommand);
		
		if(property==Property.INVALID){
			return false;
		}
		
		if(!getPlugin().getPermissionChecker().checkPermission(theCommandSender, property.getSetPermission())){
			return false;
		}
		
		try
		{
			String theValue = theArgumentList.popAll();
			switch(property){
				case ACCELERATION:
					getPlugin().getPluginConfiguration().setAcceleration(Integer.parseInt(theValue));
					theValue = getPlugin().getPluginConfiguration().getAcceleration().toString();
					break;
				case MATERIAL: 
					getPlugin().getPluginConfiguration().setMaterial(Material.getMaterial(theValue));
					theValue = getPlugin().getPluginConfiguration().getMaterial().toString();
					break;
				case MAXIMUM: 
					getPlugin().getPluginConfiguration().setMaximum(Integer.parseInt(theValue));
					theValue = getPlugin().getPluginConfiguration().getMaximum().toString();
					break;
				case MINIMUM: 
					getPlugin().getPluginConfiguration().setMinimum(Integer.parseInt(theValue));
					theValue = getPlugin().getPluginConfiguration().getMinimum().toString();
					break;
				case MESSAGE_ENABLED: 
					getPlugin().getPluginConfiguration().setMessageEnabled(Boolean.parseBoolean(theValue));
					theValue = new Boolean(getPlugin().getPluginConfiguration().getMessageEnabled()).toString();
					break;
				case MESSAGE_TEXT: 
					getPlugin().getPluginConfiguration().setMessageText(theValue);
					theValue = getPlugin().getPluginConfiguration().getMessageText();
					break;
				case PAY_SOURCE:
					getPlugin().getPluginConfiguration().setPaySource(PaySource.parsePaySource(theValue));
					theValue = getPlugin().getPluginConfiguration().getPaySource().toString();
					break;
				case CLAIMS:
					String thePlayerName = ((Player)theCommandSender).getName();
					getPlugin().getDataProvider().setPlayerClaims(thePlayerName, Integer.parseInt(theValue));
					theValue = getPlugin().getDataProvider().retrievePlayerClaims(thePlayerName).toString();
					break;
				case FILTER_STATE:
					getPlugin().getPluginConfiguration().setFilterState(FilterState.valueOf(theValue));
					theValue = getPlugin().getPluginConfiguration().getFilterState().toString();
					break;
				case FILTER_MATERIALS:
					return false;
			default:
				break;
			}
			getPlugin().sendSuccessMessage(theCommandSender, String.format(messageSuccess,property.getLabel(),theValue));
			return true;
		}catch(Exception e){
			getPlugin().getLogger().severe(e.getMessage());
			return false;
		}
	}
}
